<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;

class Cart extends Base
{
    protected $name = 'user_cart';


    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')
            ->field('id,title,price,icon');
    }

    public static function add($user, $pid, $num): array
    {
        try {
            $product = Product::where(['id' => $pid, 'status' => 1, 'is_del' => 0])->find();
            if($user->level_id>0)
            {
                $price = 'price_'.$user->level_id;
                $product->price = $product->$price;
            }else{
                $product->price = $product->market_price;
            }
            if (empty($product)) {
                return ['code' => -1, 'msg' => '商品不存在或已下架'];
            }
//            if (!$product->price || $product->price <= 0) {
//                return ['code' => -1, 'msg' => '商品价格错误'];
//            }
//            $count = parent::where('uid', $user->id)
//                ->where('cate_1', '<>', $product->cate_1)->count();
//            if ($count) {
//                return ['code' => -1, 'msg' => '购物车已有其他专区产品'];
//            }
//            if($product->cate_1!=1)
//            {
//                return ['code' => -1, 'msg' => '该专区产品不能放入购物车'];
//            }
            $map['uid'] = $user->id;
            $map['cate_1'] = $product->cate_1;
            $map['pid'] = $pid;
            $cart = parent::where($map)->find();
            if ($cart) {
                $cart->num = $cart->num + $num;
                $cart->save();
            } else {
                $map['num'] = $num;
                parent::create($map);
            }
            return ['code' => 1, 'msg' => '已添加至购物车'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function edit($type, $id, $uid): array
    {
        try {
            $msg = 'success';
            switch ($type) {
                case 1://加
                    $info = parent::where('id', $id)->find();
//                    if ($info->num >= $info->product->stock) {
//                        return ['code' => -1, 'msg' => '库存不足'];
//                    }
                    $info->num = $info->num + 1;
                    $info->save();
                    break;
                case 2://减
                    parent::where('id', $id)->dec('num')->update();
                    break;
                case 3://删
                    parent::where('id', $id)->delete();
                    $msg = '已删除';
                    break;
                case 4://清空
                    parent::where('uid', $uid)->delete();
                    $msg = '已清空';
                    break;
                default:
                    return ['code' => -1, 'msg' => '加载失败'];
            }
            return ['code' => 1, 'msg' => $msg];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function submit($param, $user): array
    {
        try {
            $all = [];
            foreach ($param['cart_id'] as $k => $v) {
                $all[$k] = parent::where('id', $v)->field('pid,num,cate_1')->find()->toArray();
            }
            Cache::set('tsOrder_' . $user->id, json_encode($all));
            Cache::set('tsOrderCartIds_' . $user->id, json_encode($param['cart_id']));

            return ['code' => 1, 'msg' => 'success', 'url' => url('order/index')->build()];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function clearCart($uid)
    {
        Cache::set('tsOrder_' . $uid, null);
        $ids = Cache::get('tsOrderCartIds_' . $uid);
        if (!empty($ids)) {
            parent::destroy(json_decode($ids, true));
        }
    }


}
